<?php

	/**
	 * @see Zend_Json_Expr
	 */
	require_once 'Zend/Json/Expr.php';
	
	/**
	 * @see Bike_Js
	 */
	require_once 'Bike/Js.php';
	
	/**
	 * @see Bike_Js_Interface_Js
	 */
	require_once 'Bike/Js/Interface/Js.php';
	
	
	class Bike_Js_Expr extends Zend_Json_Expr implements Bike_Js_Interface_Js
	{
		protected $_strCache;
		
		static public function js()
		{
			return Bike_Js::getInstance();
		}

		public function __construct()
	    {
	        $args = func_get_args();
	        $this->setOptions($this->makeOptions($args));
	    }
	    
	    public function setArgsOptions($args = array())
	    {
	    	if(!is_array($args))
	    	{
	    		$args = array($args);
	    	}
	    	$this->setOptions($this->makeOptions($args));
	    	return $this;
	    }

	    public function makeOptions(array $Args=null)
		{
			$options = array();
			if(!is_array(@$Args[0]))
			{
				$options['expr'] = @$Args[0];
			}
			else
			{				$options = $Args[0];
			}
			return $options;
		}

		public function setOptions(array $Options=null)
		{			$expression = "";
			$keys = array('expression', 'expr');
			foreach($keys as $key)
			{
				if(array_key_exists($key, $Options))
				{
					$expression = $Options[$key];
					break;
				}
			}
			$this->_expression = (string) $expression;
			return $this;
		}
		
		public function clearStrCache()
		{
			$this->_strCache = null;
		}
		
		public function __clone()
		{
			$this->clearStrCache();
		}
		
		public function __toString()
		{
			if(is_null($this->_strCache))
			{
				$this->_strCache = parent::__toString();
			}
			return $this->_strCache;
		}

		public function toJson()
		{
			return new self($this->__toString());
		}
	}
